// Examples.cpp : Defines the entry point for the console application.
//

#include <IG.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>

using namespace IG;

int main(int argc, char** argv)
{

	//int obj1 = CIG::GetInstance()->AddObject("../../../Models/VolvoV70Little.flt");
	//int world = CIG::GetInstance()->AddObject("../../../Models/floor4.flt");
	
	//visual correct path
	int obj1;// = CIG::GetInstance()->AddObject("../Models/VolvoV70Little.flt");
	int world;// = CIG::GetInstance()->AddObject("../Models/floor4.flt");
	if (argc == 2)
	{
		char buf1[256];
		sprintf(buf1, "%s/VolvoV70Little.flt", argv[1]);
		char buf2[256];
		sprintf(buf2, "%s/floor4.flt", argv[1]);
		 obj1 = CIG::GetInstance()->AddObject(buf1);
		 world = CIG::GetInstance()->AddObject(buf2);
	}
	else
	{
		 obj1 = CIG::GetInstance()->AddObject("../Models/VolvoV70Little.flt");
		 world = CIG::GetInstance()->AddObject("../Models/floor4.flt");
	}

	double  posX = 1.0;
	double  posY = -1.0;
	double  posZ = 0.0;
	double dx = 0.01;
	CameraProps Rprops;
	Rprops.borders = false;
	Rprops.farPlane = 30.0;
	Rprops.nearPlane = 0.001;
	Rprops.fovX = 90.0 * 4.0 / 5.0;
	Rprops.fovY = 90.0 * 3.0 / 5.0;
	Rprops.width = 320 * 2;
	Rprops.height = 240 * 2;
	Rprops.OffScreen = false;	   // Do not Draw on Screen
	Rprops.sensor = CAMERA_SENSOR::RGB;
	Rprops.windowOriginX = 320 + 640;
	Rprops.windowOriginY = 0;
	Rprops.multisample = 1;
	int RigthcameraKey = CIG::GetInstance()->AddCamera(Rprops);
	//Turn camera to Slave Camera
	CIG::GetInstance()->SetSlaveCamera(obj1, RigthcameraKey);
	CIG::GetInstance()->SetCameraOffset(RigthcameraKey, -0.037, 0.002, 0.12, 0, 0.0, 0.0);
	CameraProps Lprops;
	Lprops.borders = false;
	Lprops.farPlane = 30.0;
	Lprops.nearPlane = 0.001;
	Lprops.fovX = 90.0 * 4.0 / 5.0;
	Lprops.fovY = 90.0 * 3.0 / 5.0;
	Lprops.width = 320 * 2;
	Lprops.height = 240 * 2;
	Lprops.OffScreen = false;	   // Do not Draw on Screen
	Lprops.sensor = CAMERA_SENSOR::RGB;
	Lprops.windowOriginX = 320;
	Lprops.windowOriginY = 0;
	Lprops.multisample = 1;
	int LeftcameraKey = CIG::GetInstance()->AddCamera(Lprops);
	//Turn camera to Slave Camera
	CIG::GetInstance()->SetSlaveCamera(obj1, LeftcameraKey);
	CIG::GetInstance()->SetCameraOffset(LeftcameraKey, -0.043, 0.002, 0.12, 0, 0.0, 0.0);

	CIG::GetInstance()->SetObjectPosition(obj1, 1.0, -1.0, 0.0, -90.0, 0.0, 0.0);



	CameraProps Wallprops;
	Wallprops.borders = false;
	Wallprops.farPlane = 30.0;
	Wallprops.nearPlane = 0.001;
	Wallprops.fovX = 90.0 * 4.0 / 5.0;
	Wallprops.fovY = 90.0 * 3.0 / 5.0;
	Wallprops.width = 320 * 2;
	Wallprops.height = 240 * 2;
	Wallprops.OffScreen = false;	   // Do not Draw on Screen
	Wallprops.sensor = CAMERA_SENSOR::RGB;
	Wallprops.windowOriginX = 640;
	Wallprops.windowOriginY = 480;
	Wallprops.multisample = 1;
	int WallcameraKey = CIG::GetInstance()->AddCamera(Wallprops);

	CIG::GetInstance()->SetCameraLookAt(WallcameraKey, 12.0, -1.0, 1.5, 0.0, -1.0, 1.0, 0, 0, 1);



	CIG::GetInstance()->Run();
	while (1)
	{
		//1.render
		CIG::GetInstance()->Draw();

		//2.get image to do computer vision task
		cv::Mat RImage = CIG::GetInstance()->GetImage(RigthcameraKey);
		cv::Mat GrayColor = cv::Mat(RImage.size(), CV_8UC1);
		cv::cvtColor(RImage, GrayColor, CV_RGB2GRAY);

		cv::imshow("My ComputerVision", GrayColor);
		cvWaitKey(1);

		//3.update mission
		if (posX <10)
			posX += dx;
		CIG::GetInstance()->SetObjectPosition(obj1, posX, posY, posZ, -90.0, 0.0, 0.0);
	}

	return 0;
}

